import sys
from optparse import OptionParser
import logging.config
logging.config.fileConfig('logging.conf')

from urldb.view.cmdline import * # pulls in the options parser base object
from urldb.control.logparser import LogParser
from gtfsdb.web.analytics.trip.parse import atis, otp
from gtfsdb.web.analytics.trip.compare import Compare

def init():
    parser = get_parser()
    parser.add_option(
        "--csv", dest="data",
        help="CSV file name for trip parameters"
    )
    (options, args) = parser.parse_args()
    return options


def main():

    options = init()
    log = LogParser(options)
    db = log.populate
    ses = db.get_session()
    atisws = atis.CallService(db, 'http://maps5.trimet.org')
    otpws = otp.CallService(db, 'http://maps5.trimet.org')
    compare = Compare()
    comp_trips = None

    # loop through the urls, calling the service, then persisting the results
    urls = atisws.get_urls()
    for i, u in enumerate(urls):
        try:
            print "call trip url", i + 1, "of", len(urls)
            atrip = atisws.call_url(u['url'], u['record'], u['params'])
            otrip = otpws.call_via_atis(atrip)
            atrip.persist(ses)
            otrip.persist(ses)
            comp_trip = compare.compare_trips(atrip, otrip)
            comp_trip.persist(ses)
        except:
            print 'EXCEPTION: ', sys.exc_info()[0]
            #raise


    #atisws.persist()
    #otpws.persist()

    print '-->  done  <--'


if __name__ == '__main__':
    main()
